// ==UserScript==
// @name        wsmud_funny_mobile
// @namespace   suqing
// @version     0.1.1
// @author      sq
// @match       http://*.wsmud.com/*
// @homepage    https://greasyfork.org/zh-CN/scripts/382076
// @description wsmud_funny 的手机适用版
// @run-at      document-start
// @require     http://code.jquery.com/jquery-3.3.1.min.js
// @grant       unsafeWindow
// @grant       GM_addStyle
// @grant       GM_setValue
// @grant       GM_deleteValue
// @grant       GM_getValue
// @grant       GM_setClipboard
// ==/UserScript==

(function() {
    'use strict';
    let funny = {
        version: GM_info.script.version,
        data: {},
        role: {},
        data_skill_limit: 0,
        data_login: 0,
        data_id: "12345678910",
        data_jy: 0,
        data_qn: 0,
        state: "data.state",
        data_autokill_xy: true,
        layout_left: true,
    };
    unsafeWindow.funny = funny;
    let fn = {
        send: function(message) {
            if (typeof message === "string") {
                sendmessage(message);
            } else if (message instanceof Array) {
                action(message);
                async function action(message) {
                    for (const m of message) (typeof m === "number") ? (await fn.sleep(m)) : sendmessage(m);
                }
            }
            function sendmessage(message) {
                $("#sendmessage").attr("cmd") ? $("#sendmessage").attr("cmd", message) : $(".container").append($(`<span id="sendmessage" cmd="${message}"><span>`));
                $("#sendmessage").click();
            }
        },
        sleep: function(ms) {
            return new Promise(resolve => setTimeout(resolve, ms));
        },
        scroll: function(name) {
            if (name === undefined) return;
            let a = $(name)[0].scrollTop,
                b = $(name)[0].scrollHeight,
                h = Math.ceil($(name).height()); // 向上取整
                // console.log(`fn.scroll => a=${a} b=${b} h=${h}`);
            if (a < b - h) {
                let add = (b - h < 120) ? 1 : Math.ceil((b - h) / 120);
                $(name)[0].scrollTop = a + add;
                setTimeout(`fn.scroll("${name}")`, 1000/120);
            }
        },
        deepCopy: function (object) {
            let result = {};
            for (const key in object) {
                result[key] = typeof object[key] === "object" ? fn.deepCopy(object[key]) : object[key];
            }
            return result;
        },
        getTime: function() {
            let date = new Date();
            let h = parseInt(date.getHours());
            let m = parseInt(date.getMinutes());
            h = h < 10 ? `0${h}` : `${h}`;
            m = m < 10 ? `0${m}` : `${m}`;
            return `${h}:${m}`;
        },
    };
    unsafeWindow.fn = fn;
    let listener = {
        onmessage: function(message) {
            let data = message.data;
            if (/{(.*)}/.test(data)) data = (new Function(`return ${data};`))();
            if (typeof data === "string") {
                listener.extends.text.forEach(fn => fn(message));
                return;
            } else if (typeof data === "object") {
                switch (data.type) {
                    case "roles":
                        funny.data.roles = data;
                        break;
                    case "login":
                        funny.data.id = data.id;
                        break;
                    case "dialog":
                        listener.extends.dialog.forEach(fn => fn(data));
                        break;
                    case "msg":
                        funny.onmessage_fn.apply(this, arguments);
                        return;
                    case "room":
                        listener.extends.room.forEach(fn => fn(message, data));
                        return;
                    case "exits":
                        break;
                    case "item":
                        break;
                    case "items":
                        break;
                    case "itemadd":
                        listener.extends.itemadd.forEach(fn => fn(data));
                        break;
                    case "itemremove":
                        break;
                    case "actions":
                        break;
                    case "addAction":
                        break;
                    case "state":
                        funny.state = data.state;
                        listener.extends.state.forEach(fn => fn(message, data));
                        return;
                    case "perform":
                        break;
                    case "status":
                        break;
                    case "combat":
                        break;
                    case "dispfm":
                        break;
                    case "sc":
                        break;
                    case "time":
                        break;
                    case "disobj":
                        break;
                    default:
                        console.log(data);
                        break;
                }
            }
            funny.onmessage_fn.apply(this, arguments);
        },
        sendmessage: function(message) {
            funny.webSocket.send(message);
        },
        extends: {
            text: [],
            dialog: [],
            msg: [],
            room: [],
            state: [],
            itemadd: [],
        },
        addListener: function(type, fn) {
            listener.extends[type].push(fn);
        },
    };
    unsafeWindow.listener = listener;
    listener.addListener("text", function(message) {
        let data = message.data;
        if (/重新连线|欢迎登陆/.test(data)) {
            funny.data_login += 1;
            $(".content-message pre").append(`${data}\n`);
            if (funny.data_login === 1) {
                $(".content-message pre").append(`wsmud_funny_mobile ${funny.version} 苏轻祝您游戏愉快！\n`);
                getScore();
            }
            async function getScore() {
                $("[command=score]").click();
                await fn.sleep(100);
                $("[for=1]").click();
                await fn.sleep(100);
                $("[for=0]").click();
                await fn.sleep(100);
                $("[command=skills]").click();
                await fn.sleep(100);
                $("[command=pack]").click();
                await fn.sleep(100);
                $("[command=tasks]").click();
                await fn.sleep(100);
                $("[command=showcombat]").click(); // 点击动作
                await fn.sleep(100);
                $("[command=showtool]").click();   // 点击菜单
                await fn.sleep(1000);
                $(".dialog-close").click();
            };
        } else if (/你获得了(.*)点经验，(.*)点潜能/.test(data)) {
            funny.onmessage_fn.apply(this, arguments);
            let a = data.match(/你获得了(.*)点经验，(.*)点潜能/);
            funny.data_jy += parseInt(a[1]);
            funny.data_qn += parseInt(a[2]);
            $(".remove_data_jyqn").remove();
            $(".content-message pre").append(
                $(`<span class="remove_data_jyqn"></span>`)
                .append(`合计 => 经验:${funny.data_jy} 潜能:${funny.data_qn}\n`)
            );
        } else if (/你轻声吟道/.test(data)) {
        } else if (/看起来(.*)想杀死你/.test(data)) {
            let a = data.match(/看起来(.*)想杀死你！/);
            $(".content-message pre").append(`<hir>${a[1]} => 开始攻击你！<hir>\n`);
        } else if (/你对著(.*)喝道/.test(data)) {
            let a = data.match(/你对著(.*)喝道/);
            $(".content-message pre").append(`<hir>你 => 开始攻击${a[1]}！<hir>\n`);
        } else if (/你扑向(.*)/.test(data)) {
            let a = data.match(/你扑向(.*)！/);
            $(".content-message pre").append(`<hir>你 => 开始攻击${a[1]}！<hir>\n`);
        } else if (/造成(.*)点/.test(data)) {
            let a = data.split(/.*造成<wht>|.*造成<hir>|<\/wht>点|<\/hir>点/);
            let b = a[2].split(/伤害|\(|</);
            if (b[0] === "暴击") {
                $(".content-message pre").append(`${b[2]}受到<hir>${a[1]}</hir>点<hir>${b[0]}</hir>伤害！\n`);
            } else {
                $(".content-message pre").append(`${b[2]}受到<wht>${a[1]}</wht>点伤害！\n`);
            }
        } else if (/你的最大内力增加了/.test(data)) {
            funny.onmessage_fn.apply(this, arguments);
            let a = data.match(/你的最大内力增加了(.*)点。/);
            let n = parseInt(a[1]),
              max = parseInt(funny.role.max_mp),
            limit = parseInt(funny.role.limit_mp);
            let time = (limit - max) / (n * 6); // X分钟 => X小时X分钟
            let timeString = time < 60 ? `${parseInt(time)}分钟` : `${parseInt(time / 60)}小时${parseInt(time % 60)}分钟`;
            $(".remove_dzsj").remove();
            $(".content-message pre").append(`<span class="remove_dzsj">当前内力: ${max}\n上限内力: ${limit}\n需要时间: ${timeString}\n</span>`);
        } else if (/无数花瓣夹杂着寒气/.test(data)) {
            let a = data.match(/无数花瓣夹杂着寒气将(.*)围起/);
            $(".content-message pre").append(`<him>「太上忘情」 => ${a[1]}（无法躲闪）</him>\n`);
        } else if (/数息后只留下一堆玄色石头/.test(data)) {
            let a = data.match(/只见(.*)发出一阵白光/);
            $(".content-message pre").append(`你分解了 => ${a[1]}\n`);
        } else {
            funny.onmessage_fn.apply(this, arguments);
        }
        fn.scroll(".content-message");
    });

    // tasks
    listener.addListener("dialog", function(data) {
        if (data.dialog === "tasks" && data.items) {
            let fb, qa, wd1, wd2, wd3, sm1, sm2, ym1, ym2, yb1, yb2;
            data.items.forEach(item => {
                if (item.state === 2) fn.send(`taskover ${item.id}`); // 自动完成
                if (item.id === "signin") {
                    let a = item.desc.match(/师门任务：(.*)，副本：<(.*)>(.*)\/20<(.*)>/);
                    let b = item.desc.match(/(.*)武道塔(.*)，进度(.*)\/(.*)<(.*)>，<(.*)>(.*)首席请安<(.*)>/);
                    (parseInt(a[3]) < 20) ? fb = `<hig>${a[3]}</hig>` : fb = a[3];
                    (parseInt(b[3]) < parseInt(b[4])) ? wd1 = `<hig>${b[3]}</hig>` : wd1 = b[3];
                    wd2 = b[4];
                    /可以重置/.test(b[2]) ? wd3 = "<hig>可以重置</hig>" : wd3 = "已经重置";
                    /已经/.test(b[7]) ? qa = "已经请安" : qa = "<hig>尚未请安</hig>";
                } else if (item.id === "sm") {
                    let a = item.desc.match(/目前完成(.*)\/20个，共连续完成(.*)个。/);
                    (parseInt(a[1]) < 20) ? sm1 = `<hig>${a[1]}</hig>` : sm1 = a[1];
                    sm2 = a[2];
                } else if (item.id === "yamen") {
                    let a = item.desc.match(/目前完成(.*)\/20个，共连续完成(.*)个。/);
                    (parseInt(a[1]) < 20) ? ym1 = `<hig>${a[1]}</hig>` : ym1 = a[1];
                    ym2 = a[2];
                } else if (item.id === "yunbiao") {
                    let a = item.desc.match(/本周完成(.*)\/20个，共连续完成(.*)个。/);
                    (parseInt(a[1]) < 20) ? yb1 = `<hig>${a[1]}</hig>` : yb1 = a[1];
                    yb2 = a[2];
                }
            });
            let html = `门派请安 => ${qa}\n武道之塔 => ${wd1}/${wd2} ${wd3}\n`;
            html += `日常副本 => ${fb}/20\n师门任务 => ${sm1}/20 ${sm2}连\n`;
            html += `衙门追捕 => ${ym1}/20 ${ym2}连\n每周运镖 => ${yb1}/20 ${yb2}连\n`;
            $(".remove_tasks").remove();
            $(".content-message pre").append($(`<span class="remove_tasks"><span>`).html(html));
            fn.scroll(".content-message");
        }
    });

    // score
    listener.addListener("dialog", function(data) {
        if (data.dialog === "score") {
            for (const key in data) funny.role[key] = data[key];
        }
    });

    // skills
    listener.addListener("dialog", function(data) {
        if (data.dialog === "skills") {
            funny.skills = data.items || funny.skills || [];
            if (data.items) {
                funny.data_skill_limit = parseInt(data.limit);
            } else if (data.id && data.exp) {
                if (data.level) {
                    for (const skill of funny.skills) {
                        if (skill.id === data.id) {
                            skill.level = data.level;
                            break;
                        }
                    }
                }
                let name = "", k = 0, level = 0;
                let djsx = funny.data_skill_limit; // 上限
                let xxxl = parseInt(funny.role.study_per);   // 学习效率
                let lxxl = parseInt(funny.role.lianxi_per);  // 练习效率
                let xtwx = parseInt(funny.role.int);         // 先天悟性
                let htwx = parseInt(funny.role.int_add);     // 后天悟性
                if (funny.skills) {
                    for (const skill of funny.skills) {
                        if (skill.id === data.id) {
                            name = skill.name;
                            level = parseInt(skill.level);
                            if (/<wht>.*/.test(name)) k = 1; // 白
                            if (/<hig>.*/.test(name)) k = 2;
                            if (/<hic>.*/.test(name)) k = 3;
                            if (/<hiy>.*/.test(name)) k = 4;
                            if (/<hiz>.*/.test(name)) k = 5;
                            if (/<hio>.*/.test(name)) k = 6; // 橙
                            if (/<hir>.*/.test(name)) k = 7; // 红
                            break;
                        }
                    }
                }
                let qianneng = (djsx * djsx - level * level) * 2.5 * k;
                if (funny.state === "你正在练习技能") {
                    let time = qianneng / (xtwx + htwx) / (1 + lxxl / 100 - xtwx / 100) / 12;
                    let timeString = time < 60 ? `${parseInt(time)}分钟` : `${parseInt(time / 60)}小时${parseInt(time % 60)}分钟`;
                    $(".remove_lx").remove();
                    // 练习每一跳的消耗公式＝（先天悟性＋后天悟性）×（1＋练习效率%－先天悟性%）
                    $(".content-message pre").append(`练习${name}消耗了${parseInt(qianneng / time / 12)}点潜能。\n`);
                    $(".content-message pre").append(`<span class="remove_lx">角色悟性: ${xtwx}＋${htwx}\n练习效率: ${lxxl}%\n等级上限: ${djsx}级\n需要潜能: ${qianneng}\n需要时间: ${timeString}\n</span>`);
                    fn.scroll(".content-message");
                } else if (funny.state === "你正在读书") {
                    // 学习每一跳的消耗公式＝（先天悟性＋后天悟性）×（1＋学习效率%－先天悟性%）×3
                    let cost = (xtwx + htwx) * (1 +  xxxl / 100 - xtwx / 100) * 3;
                    $(".content-message pre").append(`学习${name}消耗了${parseInt(cost)}点潜能。\n`);
                    if (funny.data.id === "j9h729c52bc") {
                        let time = qianneng / cost / 12;
                        let timeString = time < 60 ? `${parseInt(time)}分钟` : `${parseInt(time / 60)}小时${parseInt(time % 60)}分钟`;
                        $(".content-message pre").append(`练满时间 => ${timeString}\n`);
                    }
                    fn.scroll(".content-message");
                }
            }
        }
    });
    // pack
    listener.addListener("dialog", function(data) {
        if (data.dialog === "pack") {

            if (data.can_use == 1 && /养精丹|朱果|潜灵果/.test(data.name)) {
                let count = data.count > 10 ? 10 : data.count;
                let cmd = [];
                for (let i = 0; i < count; i ++) {
                    cmd.push(`use ${data.id}`);
                    cmd.push(500);
                }
                $(".content-message pre").append(
                    $(`<div class="item-commands"></div>`).append(
                        $(`<span>快捷使用${count}次 => ${data.name}</span>`).click(() => {
                            fn.send(cmd);
                        }),
                    ),
                );
            }

            if (data.name) {
                if (/<hig>大宋(.*)<\/hig>|<hig>蒙古(.*)<\/hig>|<hig>笠子帽<\/hig>/.test(data.name)) {
                    fn.send(`fenjie ${data.id}`);
                }
            }
        }
    });

    listener.addListener("room", function(message, data) {
        if (/cmd cmd=/.test(data.desc)) {
            data.desc = data.desc.replace("<hig>椅子</hig>", "椅子");
            let a = data.desc.match(/<cmd cmd='([^']+)'>([^<]+)<\/cmd>/g);
            a.forEach(desc => data.desc = `<hic>${desc}</hic>　${data.desc}`);
            let mask = fn.deepCopy(message);
            mask.data = JSON.stringify(data);
            funny.onmessage_fn.apply(this, [mask]);
        } else {
            funny.onmessage_fn.apply(this, [message]);
        }
    });
    listener.addListener("state", function(message, data) {
        if (data.desc && data.desc.length > 0) {
            data.desc = [];
            let mask = fn.deepCopy(message);
            mask.data = JSON.stringify(data);
            funny.onmessage_fn.apply(this, [mask]);
        } else {
            funny.onmessage_fn.apply(this, [message]);
        }
    });

    listener.addListener("itemadd", function(data) {
        if (/蒙古兵|十夫长|百夫长|千夫长|万夫长/.test(data.name)) {
            if (data.id) fn.send(`kill ${data.id}`);
        }
    });



    if (WebSocket) {
        unsafeWindow.WebSocket = function(url) {
            funny.webSocket = new WebSocket(url);
        };
        unsafeWindow.WebSocket.prototype = {
            get url() {
                return funny.webSocket.url;
            },
            get protocol() {
                return funny.webSocket.protocol;
            },
            get readyState() {
                return funny.webSocket.readyState;
            },
            get bufferedAmount() {
                return funny.webSocket.bufferedAmount;
            },
            get extensions() {
                return funny.webSocket.extensions;
            },
            get binaryType() {
                return funny.webSocket.binaryType;
            },
            set binaryType(type) {
                funny.webSocket.binaryType = type;
            },
            get onerror() {
                return funny.webSocket.onerror;
            },
            set onerror(fn) {
                funny.webSocket.onerror = fn;
            },
            get onopen() {
                return funny.webSocket.onopen;
            },
            set onopen(fn) {
                funny.webSocket.onopen = fn;
            },
            get onclose() {
                return funny.webSocket.onclose;
            },
            set onclose(fn) {
                funny.webSocket.onclose = fn;
            },
            close: function () {
                funny.webSocket.close();
            },
            get onmessage() {
                return funny.webSocket.onmessage;
            },
            set onmessage(fn) {
                funny.onmessage_fn = fn;
                funny.webSocket.onmessage = listener.onmessage;
            },
            send: function (message) {
                listener.sendmessage(message);
            },
        };
    };

    $(document).ready(function() {
        GM_addStyle(`.room_desc{overflow:hidden;white-space:nowrap;}`);
        GM_addStyle(`.content-bottom{-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;}`);
    });
})();